﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Habi.Solver.Services;
using DevExpress.Utils.MVVM.Services;
using DotNetExtensions.AspNet.DevExpress.Services;
using DotNetExtensions.AspNet.DevExpress.MVVM.ViewModels;

namespace Habi.Solver.Common
{
    public partial class ExtensionControl<T> : XtraUserControl where T : class, new()
    {
        [Browsable(true)]
        [Description("Set default enter key down accept button"), Category("Custom"), DefaultValue(null)]
        public IButtonControl AcceptButton { get; set; }
        protected readonly IValidationService ValidationService;
        public ExtensionControl()
        {
            InitializeComponent();
            ValidationService = new ValidationService(this.dxValidationProvider1);
            mvvmContext.ViewModelType = typeof(T);
            mvvmContext.RegisterService(ValidationService);
            mvvmContext.RegisterService("FlyoutDocumentManagerService", WindowedDocumentManagerService.CreateFlyoutFormService(this));
            mvvmContext.RegisterService(new ControlOperationService(this));
            mvvmContext.RegisterService(new DialogMessageBoxService(this));
            mvvmContext.RegisterService(new HttpRequestService());
            if (!mvvmContext.IsDesignMode)
            {
                var fluent = mvvmContext.OfType<T>();
                ((IBaseViewModel)fluent.ViewModel).Readying();
                this.Load += (sender, e) =>
                {
                    if (!mvvmContext.IsDesignMode)
                    {
                        InitializeView(fluent);
                    }
                };
            }
        }

        public virtual void InitializeView(DevExpress.Utils.MVVM.MVVMContextFluentAPI<T> fluent)
        {
            
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // register accept button
            var form = this.FindForm();
            if (form != null) form.AcceptButton = AcceptButton;
        }
    }
}
